﻿using Avalonia.Media.Imaging;
using Project2004.Jigsaw.Controls;
using Project2004.Jigsaw.Models;
using Project2004.Jigsaw.Views;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Project2004.Jigsaw.Presenters
{
    public class MainPresenter
    {
        #region fields
        private MainView _view;
        private Scene _scene;

        private Bitmap _imageSource;

        private List<BlockPresenter> _blockPresenters = new List<BlockPresenter>();
        private List<BlockPresenterGroup> _blockPresenterGroups = new List<BlockPresenterGroup>();

        #endregion

        #region props

        public IReadOnlyList<BlockPresenter> BlockPresenters => _blockPresenters;
        public Scene Scene => _scene;

        #endregion

        #region ctors


        public MainPresenter(MainView view)
        {
            var imagePath = Path.Combine(Environment.CurrentDirectory, "Assets", "test.png");
            _imageSource = new Bitmap(imagePath);
            _view = view;

            _scene = new Scene(_imageSource.Size.Width, _imageSource.Size.Height, 2, 2);

            Init();
        }

        #endregion

        #region methods

        private void Init()
        {
            var view = _view;
            var scene = _scene;

            _blockPresenterGroups = new List<BlockPresenterGroup>();

            var groups = scene.GetGroups();

            foreach (var group in groups)
            {
                var blockPresenterGroup = CreateBlockPresenterGroup(group);
                _blockPresenterGroups.Add(blockPresenterGroup);
            }
        }

        private BlockPresenter CreateBlockPresenter(BlockPresenterGroup group, MovableBlock block)
        {
            var jigsawBlock = new JigsawBlock2(_imageSource, _scene, block);

            _view.PART_Canvas.Children.Add(jigsawBlock);
            //jigsawBlock.ZIndex = _view.PART_Canvas.Children.Count;
            var presenter = new BlockPresenter(block.Id, jigsawBlock, block, group);
            presenter.ZIndex = _view.PART_Canvas.Children.Count;
            return presenter;
        }

        private BlockPresenterGroup CreateBlockPresenterGroup(LogicalGroupModel groupModel)
        {
            var group = groupModel.Key;

            var blockPresenterGroup = new BlockPresenterGroup(this);

            foreach (var block in groupModel.Blocks)
            {
                var blockPresenter = CreateBlockPresenter(blockPresenterGroup, block);
                blockPresenterGroup.Blocks.Add(blockPresenter);
                _blockPresenters.Add(blockPresenter);
            }

            return blockPresenterGroup;
        }


        public void SetFocus(BlockPresenter blockPresenter)
        {
            var maxZIndex = _view.PART_Canvas.Children.Count;
            var oldZIndex = blockPresenter.ZIndex;

            foreach (var child in _blockPresenters)
            {
                if (child.ZIndex > oldZIndex) child.ZIndex = child.ZIndex - 1;
            }
            blockPresenter.ZIndex = maxZIndex;
        }

        #endregion
    }
}
